﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace StopWatch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Timer time = new Timer();
        Stopwatch sw; //秒表对象
        TimeSpan ts;
        private string timeString = "{0}:{1}:{2}:{3}";
        private void Form1_Load(object sender, EventArgs e)
        {
            labShow.Text = string.Format(timeString, "00", "00", "00", "00");
            button2.Enabled = false;
            button3.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button2.Enabled = true;
            button3.Enabled = true;
            button1.Enabled = false;
            sw = new Stopwatch();
            time.Tick += new EventHandler(Time_Tick);
            time.Interval = 1;
            sw.Start();
            time.Start();
        }

        private void Time_Tick(object sender, EventArgs e)
        {
            ts = sw.Elapsed;
            labShow.Text = string.Format(timeString, ts.Hours.ToString().PadLeft(2, '0'), ts.Minutes.ToString().PadLeft(2, '0'), ts.Seconds.ToString().PadLeft(2, '0'), ts.Milliseconds / 10);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sw.Stop();
            time.Stop();
            labShow.Text = string.Format("{0}:{1}:{2}:{3}", "00", "00", "00", "00");
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;
            button2.Text = "暂停";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "暂停")
            {
                //暂停事件按钮
                button2.Text = "继续";
                sw.Stop();
                time.Stop();
            }
            else if (button2.Text == "继续")
            {
                //继续事件
                button2.Text = "暂停";
                sw.Start();
                time.Start();
            }

        }
    }
}
